//
//  MainPageViewController.m
//  BTRIP
//
//  Created by johnbk on 15/11/12.
//  Copyright © 2015年 johnbk. All rights reserved.
//

#import "MainPageViewController.h"
#import "AppDelegate.h"
#import "TravelDiaryTableViewCell.h"
#import <AFNetworking.h>
#import <AFHTTPSessionManager.h>
#import "DataHandle.h"
#import "FirstTravelDiaryCollectionViewCell.h"
#import "FirstSpecialCollectionViewCell.h"
#import "FirstDetailTravelViewController.h"
#import "FirstSpecialCollectionViewCell.h"
#import "FirstSpecialViewController.h"
#define vBackBarButtonItemName  @"backArrow.png"    //导航条返回默认图片名
@interface MainPageViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate,FirstTravelDiaryCollectionViewCellDelegate,FirstSpecialCollectionViewCellDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UICollectionView *collectionView;
@end

@implementation MainPageViewController

- (void)dealloc{
    [_collectionView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	
	self.title = @"主界面";
	self.view.backgroundColor = [UIColor whiteColor];
    //接受消息
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nextPAge:) name:@"detail" object:nil];
    
//    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:0.576 green:0.867 blue:0.988 alpha:1],NSFontAttributeName, nil];
    
    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[@"游记",@"专题"]];
//    [segment setTitleTe0xtAttributes:dic forState:UIControlStateNormal];
    segment.backgroundColor = [UIColor whiteColor];
    segment.frame = CGRectMake(10 * SWIDTH, 68 * SWIDTH, KWIDTH - 20, 30 * SWIDTH);
    segment.tintColor = [UIColor lightGrayColor];
    [segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    segment.tag = 7000;
    segment.selectedSegmentIndex = 0;
    [self.view addSubview:segment];
    [segment release];

    
	self.automaticallyAdjustsScrollViewInsets = NO;
	UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
	menuBtn.frame = CGRectMake(0, 0, 20, 18);
	[menuBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-mulu"] forState:UIControlStateNormal];
	[menuBtn addTarget:self action:@selector(openOrCloseLeftList) forControlEvents:UIControlEventTouchUpInside];
	self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuBtn];
    
  [self creatCollectionView];
    
}
#pragma mark segment点击事件
- (void)segmentAction:(UISegmentedControl *)segment{
    switch (segment.selectedSegmentIndex) {
        case 0:
        self.collectionView.contentOffset = CGPointMake(0, 0);
        break;
        case 1:
        self.collectionView.contentOffset = CGPointMake(KWIDTH, 0);
        break;
    }
}
#pragma mark collectionView滑动事件
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger size = self.collectionView.contentOffset.x / KWIDTH;
    switch (size) {
        case 0:
        ((UISegmentedControl *)[self.view viewWithTag:7000]).selectedSegmentIndex = 0;
        break;
        case 1:
        ((UISegmentedControl *)[self.view viewWithTag:7000]).selectedSegmentIndex = 1;
        break;
    }
}


#pragma mark 创建collectionView
- (void)creatCollectionView{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    //最小间距
    flow.minimumLineSpacing = 0;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 101 * SWIDTH, KWIDTH, KHEIGHT - 101 * SWIDTH) collectionViewLayout:flow];
    
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.937 green:0.937 blue:0.957 alpha:1];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    //设置其滚动方式
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.bounces = NO;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    //注册重用标识
    [self.collectionView registerClass:[FirstTravelDiaryCollectionViewCell class] forCellWithReuseIdentifier:@"firstCell"];
    [self.collectionView registerClass:[FirstSpecialCollectionViewCell class] forCellWithReuseIdentifier:@"secondCell"];
    
}
#pragma mark item的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        FirstTravelDiaryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"firstCell" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }else{
        FirstSpecialCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"secondCell" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(KWIDTH, KHEIGHT - 101);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)nextPage:(NSString *)ID
{
    FirstDetailTravelViewController *firstDetail = [[FirstDetailTravelViewController alloc] init];
    firstDetail.ID = ID;
    [self.navigationController pushViewController:firstDetail animated:YES];
}

- (void)nextSpecialPage:(NSString *)ID{
    FirstSpecialViewController *firstSpecial = [[FirstSpecialViewController alloc]init];
    firstSpecial.ID = ID;
    [self.navigationController pushViewController:firstSpecial animated:YES];
}


// 消息中心
//- (void)nextPAge:(NSNotification *)info
//{
//    FirstDetailTravelViewController *detail = [[FirstDetailTravelViewController alloc] init];
//    detail.ID = info.object;
//   
//    [self.navigationController pushViewController:detail animated:YES];
//}

- (void) openOrCloseLeftList
{
	AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
	
	if (tempAppDelegate.leftSlideVC.closed)
	{
		[tempAppDelegate.leftSlideVC openLeftView];
	}
	else
	{
		[tempAppDelegate.leftSlideVC closeLeftView];
	}
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
	
	AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
	[tempAppDelegate.leftSlideVC setPanEnabled:NO];
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	
	AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
	[tempAppDelegate.leftSlideVC setPanEnabled:YES];

    [self.navigationController setNavigationBarHidden:NO];

	[self.navigationController.navigationBar setHidden:NO];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
